package com.ev.custom.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ev.custom.dao.DieArchivesMapper;
import com.ev.custom.domain.DieArchivesDO;
import com.ev.custom.service.DieArchivesPageParam;
import com.ev.custom.service.DieArchivesService;
import com.ev.custom.vo.DieArchivesParam;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 文件名称： DieArchivesServiceImpl.java
 * 初始作者： 胡</br>
 * 创建日期： 2021/4/1 9:54</br>
 * 功能说明： 【模具档案 服务实现类】 <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者 日期 修改内容<br/>
 * ================================================<br/>
 * Copyright (c) 2020-2020 .All rights reserved.<br/>
 */
@Service
public class DieArchivesServiceImpl extends ServiceImpl<DieArchivesMapper, DieArchivesDO> implements DieArchivesService {

    @Override
    public IPage<DieArchivesDO> list(DieArchivesPageParam param) {

        Page<DieArchivesDO> page = new Page<DieArchivesDO>(param.getCurrentPage(), param.getPageSize());
        QueryWrapper<DieArchivesDO> queryWrapper = new QueryWrapper<DieArchivesDO>();
        //状态
        queryWrapper.like(!StringUtils.isEmpty(param.getDieArchivesCode()),"die_archives_code",param.getDieArchivesCode());
        queryWrapper.eq("del_flag", 0);
        queryWrapper.orderByDesc("create_time");
        IPage<DieArchivesDO> result = this.baseMapper.selectPage(page, queryWrapper);
        return result;
    }

    @Override
    public DieArchivesDO detail(Long id) {
        return this.baseMapper.selectById(Long.valueOf(id));
    }

    @Override
    public JSONObject audit(DieArchivesParam param) throws Exception {
        if(CollectionUtils.isEmpty(param.getIds())){
            throw new Exception("请选择数据！");
        }
        QueryWrapper<DieArchivesDO> queryWrapper = new QueryWrapper<DieArchivesDO>();
        queryWrapper.in("id",param.getIds());
        queryWrapper.eq("del_flag",0);
        queryWrapper.last("for update");
        List<DieArchivesDO> archives = this.baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(archives)){
            throw new Exception("待处理数据不存在！");
        }
        for (DieArchivesDO archive : archives) {
            if("0".equals(param.getOperation())){//审核
                if(Objects.equals(0,archive.getStatus())){
                    archive.setStatus(1);//设置为已审核
                }else{
                    throw new Exception("只能审核未审核的数据,模具编号："+archive.getDieArchivesCode());
                }
            }
            if("1".equals(param.getOperation())){//反审核
                if(Objects.equals(1,archive.getStatus())){
                    archive.setStatus(0);//设置为未审核
                }else{
                    throw new Exception("只能反审核已审核的数据,模具编号："+archive.getDieArchivesCode());
                }
            }
            updateById(archive);
        }
        JSONObject result = new JSONObject();
        result.put("success", true);
        return result;
    }

    @Override
    public JSONObject saveUpdate(DieArchivesDO param) throws Exception {
        //检查模具编号是否重复


        if(ObjectUtils.isEmpty(param.getId())){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("del_flag",0);
            queryWrapper.eq("die_archives_code",param.getDieArchivesCode());
            param.setStatus(0);
            List<DieArchivesDO> list = this.baseMapper.selectList(queryWrapper);
            if(CollectionUtils.isEmpty(list)){
                save(param);
            }else{
                throw new Exception("模具编号已存在,模具编号："+param.getDieArchivesCode());
            }
        }else{
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("del_flag",0);
            queryWrapper.eq("die_archives_code",param.getDieArchivesCode());
            queryWrapper.ne("id",param.getId());
            List<DieArchivesDO> list = this.baseMapper.selectList(queryWrapper);
            if(CollectionUtils.isEmpty(list)){
                DieArchivesDO dieArchivesDO = this.baseMapper.selectById(param.getId());
                if(!Objects.equals(0,dieArchivesDO.getStatus())){
                    throw new Exception("模具已审核,无法修改!模具编号："+param.getDieArchivesCode());
                }

                updateById(param);
            }else{
                throw new Exception("模具编号已存在,模具编号："+param.getDieArchivesCode());
            }
        }
        JSONObject result = new JSONObject();
        result.put("success", true);
        return result;
    }

    @Override
    public void del(Long[] ids) throws Exception {
        List idList = CollectionUtils.arrayToList(ids);
        QueryWrapper<DieArchivesDO> queryWrapper = new QueryWrapper<DieArchivesDO>();
        queryWrapper.in("id",idList);
        queryWrapper.eq("del_flag",0);
        queryWrapper.last("for update");
        List<DieArchivesDO> archives = this.baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(archives)){
            throw new Exception("待处理数据不存在！");
        }
        for (DieArchivesDO archive : archives) {
            if(Objects.equals(0,archive.getStatus())){
                removeById(archive.getId());
            }else{
                throw new Exception("只能删除未审核的数据,模具编号："+archive.getDieArchivesCode());
            }
        }
    }
}
